$(function() {
	var basePath = sessionStorage.getItem("basePath");
	var $ctx = basePath + "/api";
	var username = sessionStorage.getItem("username");
	var userId = sessionStorage.getItem("userId");
	var userImg = sessionStorage.getItem("userImg");
	if(userId == null || userId == undefined){
		userId = 10000;username="管理员";userImg="static/img/fire-1.jpg";
	}else{
		console.log("当前用户====" + username);
	}
	$("#username").text(username);
	$("#userImg").attr('src',userImg);
	console.log(username);
	createUserList();
	
	function createUserList(){
		$.ajax({
			type: "get",
			url: $ctx + "/sys/findUserList",
			data: {
				'userId': userId
			},
			success: function(data) {
				console.log(data);
				//加载用户
				$("#userList").html("");
				for(i = 0; i < data.userList.length; i++) {
					var user = data.userList[i];
					var mrImg = "mui/images/cbd.jpg";
					if(user.img != undefined && user.img != "") {
						mrImg = user.img;
					}
					var str = "<li class='mui-table-view-cell mui-media'><a href='javascript:;'>";
					if(user.status == 1){
						str = str + "<img class='mui-media-object mui-pull-left' src='" + mrImg + "'>";
					}else{
						str = str + "<img class='mui-media-object mui-pull-left gray' src='" + mrImg + "'>";
					}
					str = str + "<div class='mui-media-body' id="+user.id+" name=" + user.name + ">" + user.name;
					str = str + "<p class='mui-ellipsis'>最近聊天...</p></div></a></li>";
	
					$("#userList").append(str);
					
					sessionStorage.setItem('img_'+user.id, mrImg); //缓存img地址
				}
				$(".mui-media-body").on("click", function(item) {
					/*mui.openWindow({
						url:'chat-winds.html',
						id:'chat-winds.html',
						extras: {name:'mui'}
					})*/
					var url = 'chat-winds.html?friendId=' + $(this).attr("id") + '&friendName=' + $(this).attr("name");
					window.location.href = url;
				})
			}
		});
	}

	//webSocket
	var createNum = 0;
	function createSocket(){
		// 实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
        var socketUrl= basePath + "/im/" + userId;
		socketUrl = socketUrl.replace("https", "ws").replace("http", "ws");
		console.log(socketUrl)
		var socket = new WebSocket(socketUrl);
		//打开事件
		socket.onopen = function() {
			console.log("websocket已打开");
			//createSocket();
			//socket.send("这是来自客户端的消息" + location.href + new Date());
		};
		//获得消息事件
		socket.onmessage = function(msg) {
			console.log("获取消息：" + msg.data);
			var objson = JSON.parse(msg.data);
			console.log("code：" + objson.code);
			if(objson.code == 100 || objson.code == 300){
				console.log("刷新好友")
				createUserList();
			}
		};
		//关闭事件
		socket.onclose = function() {
			console.log("websocket已关闭");
			if(createNum < 5){
				setTimeout(function() {
					createSocket();
				}, 3000);
			}
		};
		//发生了错误事件
		socket.onerror = function() {
			console.log("websocket发生了错误" + createNum);
			if(createNum < 5){
				setTimeout(function() {
					createNum = createNum + 1;
					createSocket();
				}, 3000);
			}
		}
	}
	
	//建立socket连接
	if(typeof(WebSocket) != "undefined") {
        createSocket();
    }else{
    	console.log("您的浏览器不支持WebSocket");
	}
})